import LogUtil from '../common/utils/logUtil';
import formBindingData from '@ohos.application.formBindingData';
import formInfo from '@ohos.application.formInfo';
import { DistScheduleService } from '../common/service/distScheduleService'
import formProvider from '@ohos.application.formProvider';
import { StorageService } from '../common/service/storageService';
import http from '@ohos.net.http';

const TAG = 'card'

/**
 * 获取个人设备列表
 */
async function getDevices(userId: string) {

    if (userId != '') {

        let res = await DistScheduleService.getDeviceListByUserId(userId)
        LogUtil.info(TAG, 'res.data' + JSON.stringify(res.data))
        let devices = res.data.result
        //得到所有的设备
        for (let device of devices) {
            LogUtil.info(TAG, device.id)
        }

    }
}


async function updateForm(formId: string, userId: string) {

    try {

        let obj = {
            "array": getDevices(userId),
        };
        let formData = formBindingData.createFormBindingData(obj);
        formProvider.updateForm(formId, formData).catch((err) => {

        });

    } catch (err) {
        LogUtil.error(TAG, `failed to updateForm, err: ${JSON.stringify(err)}`);
    }
}

export enum RequestMethod {
    OPTIONS = "OPTIONS",
    GET = "GET",
    HEAD = "HEAD",
    POST = "POST",
    PUT = "PUT",
    DELETE = "DELETE",
    TRACE = "TRACE",
    CONNECT = "CONNECT"
}
//
// async function getDeviceListByUserId(userId: string) {
//
//    let httpRequest = http.createHttp();
//    let data = await httpRequest.request(
//      `${DistScheduleService.distScheduleServer}/device/userId/${userId}`,
//      {
//        method: RequestMethod.GET,
//        header: {
//          'Content-Type': 'application/json'
//        },
//        readTimeout: DistScheduleService.readTimeout,
//        connectTimeout: DistScheduleService.connectTimeout
//      }
//    );
//
//    // @ts-ignore
//    let result = JSON.parse(data.result)
//    if (result.success) {
//      return result.result
//    }
//    throw new Error(result.message)
//  }


export default {
    data: {
        userId: ""
    },

    onCreate(want) {

        this.userId = StorageService.getUserIdSync()

        let obj = {
            "devices": getDevices(this.userId),
        };
        let formData = formBindingData.createFormBindingData(obj);
        return formData;
    },

    onCastToNormal(formId) {
        // Called when the form provider is notified that a temporary form is successfully
        // converted to a normal form.
    },

    onUpdate(formId) {
        // Called to notify the form provider to update a specified form.
        updateForm(formId, this.userId);
    },

    onVisibilityChange(newStatus) {
        // Called when the form provider receives form events from the system.
    },

    onEvent(formId, message) {
        // Called when a specified message event defined by the form provider is triggered.
    },

    onDestroy(formId) {
        // Called to notify the form provider that a specified form has been destroyed.
    },

    onAcquireFormState(want) {
        // Called to return a {@link FormState} object.
        return formInfo.FormState.READY;
    }
};